package aigle.idm.diagraph.tp4.diagram_default_root.edit.parts;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ConstrainedToolbarLayoutEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;

/**
 * @generated
 */
public class ProcesseurEditPart extends ShapeNodeEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 2001;

	/**
	 * @generated
	 */
	protected IFigure contentPane;

	/**
	 * @generated
	 */
	protected IFigure primaryShape;

	/**
	 * @generated
	 */
	public ProcesseurEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(
				EditPolicyRoles.SEMANTIC_ROLE,
				new aigle.idm.diagraph.tp4.diagram_default_root.edit.policies.ProcesseurItemSemanticEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
		// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
	}

	/**
	 * @generated
	 */
	protected LayoutEditPolicy createLayoutEditPolicy() {

		ConstrainedToolbarLayoutEditPolicy lep = new ConstrainedToolbarLayoutEditPolicy() {

			protected EditPolicy createChildEditPolicy(EditPart child) {
				if (child.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE) == null) {
					if (child instanceof ITextAwareEditPart) {
						return new aigle.idm.diagraph.tp4.diagram_default_root.edit.policies.Tp4TextSelectionEditPolicy();
					}
				}
				return super.createChildEditPolicy(child);
			}
		};
		return lep;
	}

	/**
	 * (DIAGRAPH uses Iaml code) Jevon modification: we extend createNodeShape to use our extended class.
	 * @generated
	 */
	protected IFigure createNodeShape() {
		ProcesseurFigure figure = new ExtendedProcesseurFigure();
		return primaryShape = figure;
	}

	/**
	 * @generated
	 */
	public ProcesseurFigure getPrimaryShape() {
		return (ProcesseurFigure) primaryShape;
	}

	/**
	 * (DIAGRAPH uses Iaml code) Extends the normal generated Figure class with one that can handle notification events, to render changes in the model.
	 * 
	 * Unfortunately, GMF generates the Viewmap as part of the .gmfgen directly, so we cannot modify the generated code here; we have to extend it in this way. Its not ideal.
	 * @generated
	 */
	public class ExtendedProcesseurFigure extends ProcesseurFigure {

		/**
		 * To be removed when GMF graph will support multiline labels
		 */
		/**
		 * (DIAGRAPH MODIFIED 121104)
		 * @generated
		 */
		public void add(IFigure figure, Object constraint, int index) {
			if (figure instanceof WrappingLabel) {
				((WrappingLabel) figure).setTextWrap(true);
			}
			super.add(figure, constraint, index);
		}

		/**
		 * (DIAGRAPH uses Iaml code)
		 * @generated
		 */
		private EObject resolvedObject;

		/**
		 * (DIAGRAPH uses Iaml code)
		 * @generated
		 */
		public ExtendedProcesseurFigure() {
			// initialise parent as normal
			super();

			// find the parent object
			resolvedObject = resolveSemanticElement();

			// refresh any parent labels
			refreshLabels();

			// add a notification listener
			// based on http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/msg12297.html

			/*
			org.eclipse.emf.transaction.NotificationFilter nf = org.eclipse.emf.transaction.NotificationFilter.createFeatureFilter(
			    	org.openiaml.model.model.ModelPackage.eINSTANCE.getNamedElement_Name()
			    ).or(org.eclipse.emf.transaction.NotificationFilter.createFeatureFilter(
			    	org.eclipse.emf.ecore.EcorePackage.eINSTANCE.getENamedElement_Name()
			    ));
			    
			
			org.eclipse.emf.transaction.ResourceSetListener rsl = new org.eclipse.emf.transaction.ResourceSetListenerImpl(nf) {
				EXPAND xpt::Common::generatedMemberComment('(DIAGRAPH uses Iaml code)') 		
				@Override
				public void resourceSetChanged(org.eclipse.emf.transaction.ResourceSetChangeEvent event) {
					refreshLabels();
				}
			};
			
			getEditingDomain().addResourceSetListener(rsl);
			
			 */

		}

		/**
		 * (DIAGRAPH uses Iaml code)
		 * @generated
		 */
		public void refreshLabels() {
			if (resolvedObject != null) {
				// labels from parent
				if (resolvedObject.eContainer() != null) {
					EObject parent = resolvedObject.eContainer();

					// permit parent labels from ENamedElements as well
					/*
					String parentName = null;
					if (parent instanceof org.openiaml.model.model.NamedElement) {
						parentName = "Container: " + ((org.openiaml.model.model.NamedElement) parent).getName();
					} else 
					
					if (parent instanceof org.eclipse.emf.ecore.ENamedElement) {
						parentName = "Container: " + ((org.eclipse.emf.ecore.ENamedElement) parent).getName();
					}
					 */

					String containmentName = resolvedObject
							.eContainingFeature() == null ? "" : resolvedObject
							.eContainingFeature().getName();

					/* parent features */
					//{template-parents}

					/* containment features */
					//{template-containments}
				}

				// other labels from the current object

				/* type features */
				//{template-types}

				/* eType features */
				//{template-etypes}

				/* stereotype figures */
				//{template-stereotypes}
			}
		}

	}

	/**
	 * @generated
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurNomEditPart) {
			((aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurNomEditPart) childEditPart)
					.setLabel(getPrimaryShape().getFigureProcesseurNomFigure());
			return true;
		}
		if (childEditPart instanceof aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurCadenceEditPart) {
			((aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurCadenceEditPart) childEditPart)
					.setLabel(getPrimaryShape()
							.getFigureProcesseurCadenceFigure());
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurNomEditPart) {
			return true;
		}
		if (childEditPart instanceof aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurCadenceEditPart) {
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		if (addFixedChild(childEditPart)) {
			return;
		}
		super.addChildVisual(childEditPart, -1);
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * @generated
	 */
	protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
		return getContentPane();
	}

	/**
	 * DIAGRAPH modified 4
	 * @generated
	 */
	protected NodeFigure createNodePlate() {
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(20, 20);

		/*120702 120704a

		 self.getDiagram().isPixelMapMode()
		 AAA-- EXPAND xpt::Common::copyright FOR editorGen: 	  BBB-- getDiagram().domainDiagramElement.genPackage.prefix: Tp4  ////
		 CCC-- getDiagram().domainDiagramElement.ecoreClass.name: Infrastructure  ////
		 DDD-- getDiagram().domainDiagramElement.ecoreClass.references [org.eclipse.emf.ecore.impl.EReferenceImpl@af7bde (name: calculateurs) (ordered: true, unique: true, lowerBound: 0, upperBound: -1) (changeable: true, volatile: false, transient: false, defaultValueLiteral: null, unsettable: false, derived: false) (containment: true, resolveProxies: true), org.eclipse.emf.ecore.impl.EReferenceImpl@ed387b (name: peripheriques) (ordered: true, unique: true, lowerBound: 0, upperBound: -1) (changeable: true, volatile: false, transient: false, defaultValueLiteral: null, unsettable: false, derived: false) (containment: true, resolveProxies: true), org.eclipse.emf.ecore.impl.EReferenceImpl@17283a (name: localisations) (ordered: true, unique: true, lowerBound: 0, upperBound: -1) (changeable: true, volatile: false, transient: false, defaultValueLiteral: null, unsettable: false, derived: false) (containment: true, resolveProxies: true), org.eclipse.emf.ecore.impl.EReferenceImpl@52304d (name: documentation) (ordered: true, unique: true, lowerBound: 0, upperBound: 1) (changeable: true, volatile: false, transient: false, defaultValueLiteral: null, unsettable: false, derived: false) (containment: true, resolveProxies: true)]  ////
		 EEE-- getDiagram().editorGen.extension() org.isoe.diagraph.diagraph.impl.DGraphImpl@e4462f (graphHandler: null, layerName: null, layerType: null) ////
		 FFF-- getDiagram().editorGen.extensionToString() org.isoe.diagraph.diagraph.impl.DGraphImpl@e4462f (graphHandler: null, layerName: null, layerType: null) ////
		 GGG-- EXPAND MetaModel::QualifiedClassName FOR getDiagram().domainDiagramElement aigle.idm.diagraph.tp4.Infrastructure  ////
		 HHH-- getDocumentRoot(self) 	  III-- EXPAND foobar FOR getDiagram() 
		 //1 
		 //2 
		 //3aigle.idm.diagraph.tp4.Infrastructure
		 //4
		 JJJ-- getDiagram().editorGen.diagraphSize()org.eclipse.emf.ecore.impl.DynamicEObjectImpl@1c3e7fb (eClass: org.eclipse.emf.ecore.impl.EClassImpl@b31cf4 (name: OclInvalid_Class) (instanceClassName: null) (abstract: false, interface: false))  //FP120703 
		 KKK-- getGraphicalNodeEditPolicyQualifiedClassName() aigle.idm.diagraph.tp4.diagram_default_root.edit.policies.ProcesseurGraphicalNodeEditPolicy ////
		 LLL-- editPartClassName ProcesseurEditPart ////
		 LLLx-- editPartClassName.substring(1, editPartClassName.rfind('EditPart')) Processeur ////
		 MMM-- getDiagram().editPartsPackageName aigle.idm.diagraph.tp4.diagram_default_root.edit.parts
		 NNN-- getDiagram().domainDiagramElement.ecoreClass org.eclipse.emf.ecore.impl.EClassImpl@2543d4 (name: Infrastructure) (instanceClassName: null) (abstract: false, interface: false)
		 OOO-- EXPAND tototata //a
		 //b
		
		 //e  NONE
		
		
		 PPP-- getDiagram().editorGen.isDiagraphPointOfView('Diagram')false  //FP120703 
		 QQQ-- getDiagram().editorGen.isDiagraphPointOfView('Classroom')false  //FP120703 
		 RRR-- getDiagram().editorGen.isDiagraphPointOfView(nodeName())false  //FP120703  
		 TTT-- IF getDiagram().editorGen.diagraphHasNodeEdges(nodeName()
		 TTT-- has edges for:Processeur
		 hasEdges     
		 UUU-- containments for:Processeur      
		
		 getDiagram().editorGen.logDNode(nodeName())  dNode is undefined 111    
		 getDiagram().editorGen.logBackgroundColor(nodeName())  115 dNode is undefined 115 for org.eclipse.emf.ecore.impl.DynamicEObjectImpl@1c3e7fb (eClass: org.eclipse.emf.ecore.impl.EClassImpl@b31cf4 (name: OclInvalid_Class) (instanceClassName: null) (abstract: false, interface: false))      
		 VVVV-- getBackgroundColor:error
		
		
		
		 SSS-- compartments for:Processeur 
		
		 */

		//org.eclipse.emf.ecore.impl.DynamicEObjectImpl@1c3e7fb (eClass: org.eclipse.emf.ecore.impl.EClassImpl@b31cf4 (name: OclInvalid_Class) (instanceClassName: null) (abstract: false, interface: false))

		//DIAGRAPH to modify 0704
		/*  example: 
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(600,300);
		return result;
		 */
		return result;
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated
	 */
	protected NodeFigure createNodeFigure() {
		NodeFigure figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		contentPane = setupContentPane(shape);
		return figure;
	}

	/**
	 * Default implementation treats passed figure as content pane.
	 * Respects layout one may have set for generated figure.
	 * @param nodeShape instance of generated figure class
	 * @generated
	 */
	protected IFigure setupContentPane(IFigure nodeShape) {
		if (nodeShape.getLayoutManager() == null) {
			ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
			layout.setSpacing(5);
			nodeShape.setLayoutManager(layout);
		}
		return nodeShape; // use nodeShape itself as contentPane
	}

	/**
	 * @generated
	 */
	public IFigure getContentPane() {
		if (contentPane != null) {
			return contentPane;
		}
		return super.getContentPane();
	}

	/**
	 * @generated
	 */
	protected void setForegroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setForegroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setBackgroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setBackgroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineWidth(int width) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineWidth(width);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineType(int style) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineStyle(style);
		}
	}

	/**
	 * @generated
	 */
	public EditPart getPrimaryChildEditPart() {
		return getChildBySemanticHint(aigle.idm.diagraph.tp4.diagram_default_root.part.Tp4VisualIDRegistry
				.getType(aigle.idm.diagraph.tp4.diagram_default_root.edit.parts.ProcesseurNomEditPart.VISUAL_ID));
	}

	/**
	 * @generated
	 */
	public class ProcesseurFigure extends RoundedRectangle {

		/**
		 * @generated
		 */
		private WrappingLabel fFigureProcesseurNomFigure;
		/**
		 * @generated
		 */
		private WrappingLabel fFigureProcesseurCadenceFigure;

		/**
		 * @generated
		 */
		public ProcesseurFigure() {

			ToolbarLayout layoutThis = new ToolbarLayout();
			layoutThis.setStretchMinorAxis(true);
			layoutThis.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);

			layoutThis.setSpacing(0);
			layoutThis.setVertical(true);

			this.setLayoutManager(layoutThis);

			this.setCornerDimensions(new Dimension(getMapMode().DPtoLP(8),
					getMapMode().DPtoLP(8)));
			this.setForegroundColor(ColorConstants.black);
			this.setBackgroundColor(ColorConstants.white);
			createContents();
		}

		/**
		 * @generated
		 */
		private void createContents() {

			fFigureProcesseurNomFigure = new WrappingLabel();

			fFigureProcesseurNomFigure.setText("");

			fFigureProcesseurNomFigure.setFont(FFIGUREPROCESSEURNOMFIGURE_FONT);

			this.add(fFigureProcesseurNomFigure);

			fFigureProcesseurCadenceFigure = new WrappingLabel();

			fFigureProcesseurCadenceFigure.setText("");

			fFigureProcesseurCadenceFigure
					.setFont(FFIGUREPROCESSEURCADENCEFIGURE_FONT);

			this.add(fFigureProcesseurCadenceFigure);

		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureProcesseurNomFigure() {
			return fFigureProcesseurNomFigure;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureProcesseurCadenceFigure() {
			return fFigureProcesseurCadenceFigure;
		}

	}

	/**
	 * @generated
	 */
	static final Font FFIGUREPROCESSEURNOMFIGURE_FONT = new Font(
			Display.getCurrent(), "Tahoma", 12, SWT.ITALIC);

	/**
	 * @generated
	 */
	static final Font FFIGUREPROCESSEURCADENCEFIGURE_FONT = new Font(
			Display.getCurrent(), "Tahoma", 12, SWT.ITALIC);

}
